<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<style>
	.TDL{
		text-align:right;
		height:28px;
	}
	.TDC{
		height: 28px;
	}
</style>
<script type="text/javascript">
$(function(){
	 //增加隔行变色效果
	$(".listtable").zebra(); 
	
	//保存按钮初始化的时候不可用
	$('#btnSave').unbind("click");
	$('#btnSave').addClass("l-btn-disabled");
	
	$('#btnCheck').unbind("click");
	$('#btnCheck').bind('click',checkDatasource);
	
	$('#datasource_dbtype').combobox({
		onSelect:function(){
			returnDriver($(this).combobox('getValue'));
		}
	});
});

/**
 * 保存数据源配置信息
 */
function save()
{
	if ($('#datasource_add_form').form("validate"))
	{
		$.ajax({
			type:"post",
			url:'${pageContext.request.contextPath}/basemgr/datasourceAction!checkDatasource.action',
			data:$('#datasource_add_form').serialize(),
			dataType:"json",
			timeout:10*1000,
			beforeSend:function(){
				$.messager.progress({title : '提示',text : '正在保存，请稍候....'});
			},
			error:function(){
				$.messager.progress('close');
				changeSaveBtnStyle(false);
				$.messager.show({title:'提示',msg:'数据源配置不正确，请检查！'});
			},
			success:function(result){
				if(result){
					$('#datasource_add_form').form('submit', {   
						url:'${pageContext.request.contextPath}/basemgr/datasourceAction!savaDatasource.action',
						success:function(result){
							$.messager.progress('close');
							var data=$.parseJSON(result);
							//显示提示信息
							window.top.$.messager.show({title:'提示',msg:data.returnMsg});
							//调用dialog的关闭方法
							$("#addDiv").dialog('close');
						}  
					}); 
				}else{
					$.messager.progress('close');
					changeSaveBtnStyle(false);
					$.messager.show({title:'提示',msg:'数据源配置不正确，请检查！'});
				}
			}
			
		});
	}
}

//校验数据源配置是否正确
function checkDatasource(){
	if ($('#datasource_add_form').form("validate"))
	{
		$.ajax({
			type:"post",
			url:'${pageContext.request.contextPath}/basemgr/datasourceAction!checkDatasource.action',
			data:$('#datasource_add_form').serialize(),
			dataType:"json",
			timeout:10*1000,
			beforeSend:function(){
				$.messager.progress({title : '提示',text : '正在验证，请稍候....'});
			},
			error:function(){
				$.messager.progress('close');
				changeSaveBtnStyle(false);
				$.messager.show({title:'提示',msg:'数据源配置不正确，请检查！'});
			},
			complete:function(){
				$.messager.progress('close');
			},
			success:function(result){
				if(result){
					changeSaveBtnStyle(true);
					$.messager.show({title:'提示',msg:'数据源配置正确！'});
				}else{
					changeSaveBtnStyle(false);
					$.messager.show({title:'提示',msg:'数据源配置不正确，请检查！'});
				}
			}
			
		});
	}
}

/**
 * 根据数据源类型来设置驱动名、url、测试sql
 * @param dbType 数据源类型
 */
function returnDriver(dbType){
	var driverName='';
	var jdbcurl='';
	var sql = '';
	
	if(dbType=='DB2'){
		driverName='com.ibm.db2.jcc.DB2Driver';
		jdbcurl='jdbc:db2://#ip#:#port#/dbName';
		sql='select 1 from sysibm.sysdummy1';
		
	}else if(dbType=='ORACLE'){
		driverName='oracle.jdbc.driver.OracleDriver';
		jdbcurl='jdbc:oracle:thin:@#ip#:#port#:instanceName';
		sql = 'select 1 from dual';
	}
	else if (dbType == 'GREENPLUM')
	{
		driverName='org.postgresql.Driver';
		jdbcurl='jdbc:postgresql://#ip#:#port#/instanceName';
		sql = 'select 1';
	}
	$('#datasource_jdbcdrivername').val(driverName);
	$('#datasource_jdbcurl').val(jdbcurl);
	$("#datasource_sql").val(sql);
}

/**
 * 根据flag来改变【保存】是否可用
 * @param flag true:【保存】可用  false:【保存】不可用
 */
function changeSaveBtnStyle(flag)
{
	 if (flag == true)
	 {
		 $('#btnSave').removeClass("l-btn-disabled");
		 $('#btnSave').unbind("click");
		 $('#btnSave').bind("click", save);
	 }
	 else
	 {
		 $('#btnSave').addClass("l-btn-disabled");
		 $('#btnSave').unbind("click");
	 }
}
 
</script>
<form id="datasource_add_form" method="post">
	<input type="hidden" id="datasource_datasourceid" name="datasourceid" value="${datasourceid}"/>
	<input type="hidden" id="datasource_sql" name="sql" value=""/>
 	<table border="0" cellpadding="0" cellspacing="0" style="width:100%;" class="table listtable">
	    <tr>	      	
	      	<td class="TDL"><span style="color:red;font-size:15px">*</span>数据源名称：&nbsp;</td>
	      	<td class="TDC"><input type="text" id="datasource_datasourcename" name="datasourcename" class="easyui-validatebox" 
	      	data-options="required:true,missingMessage:'数据源名称不能为空',invalidMessage:'数据源名称已存在',
	      	validType:['length[1,100]', 'specialCharFilter', 'remote[\'${pageContext.request.contextPath}/basemgr/datasourceAction!checkNameIsValid.action\', \'datasourcename\']']" style="width:150px;"/></td>
	     	<td class="TDL"><span style="color:red;font-size:15px">*</span>数据库类型：&nbsp;</td>
	      	<td class="TDC">
	      		 <input id="datasource_dbtype" name="dbtype" class="easyui-combobox" style="width:150px;" data-options="
					required:true,
					panelHeight:'auto',
					valueField: 'label',
					textField: 'value',
					editable:false,
					data: [{
						label: 'DB2',
						value: 'DB2'
					},{
						label: 'ORACLE',
						value: 'ORACLE'
					},
					{
						label: 'GREENPLUM',
						value: 'GREENPLUM'
					}
					],
					missingMessage:'数据库类型不能为空'" />	
	      	</td>
	     </tr>
	     <tr>	     	
	     	<td class="TDL"><span style="color:red;font-size:15px">*</span>JDBC驱动名：&nbsp;</td>          
	      	<td class="TDC" colspan="3"><input type="text" id="datasource_jdbcdrivername" name="jdbcdrivername" class="easyui-validatebox" data-options="required:true,missingMessage:'JDBC驱动名不能为空',validType:'length[1,50]'" style="width:444px;" value=""/></td>
	     </tr>
	     <tr>
	      <td class="TDL"><span style="color:red;font-size:15px">*</span>JDBC连接串：&nbsp;</td>
	      <td class="TDC" colspan="3">
	      	 <input type="text" id="datasource_jdbcurl" name="jdbcurl" class="easyui-validatebox" data-options="required:true,missingMessage:'JDBC连接串不能为空',validType:'length[1,300]'" style="width:444px;" value=""/>
	      </td> 
	     </tr>
	     <tr>
	     	<td class="TDL"><span style="color:red;font-size:15px">*</span>用户名：&nbsp;</td>
	        <td class="TDC">
	      	   <input type="text" id="datasource_username" name="username" class="easyui-validatebox" data-options="required:true,missingMessage:'用户名不能为空',validType:['letterOnly','length[1,50]']" style="width:150px;"/>
	        </td>
	        <td class="TDL"><span style="color:red;font-size:15px">*</span>密 码：&nbsp;</td>
	        <td class="TDC">
	      	   <input type="password" id="datasource_password" name="password" class="easyui-validatebox" data-options="required:true,missingMessage:'密码不能为空',validType:'length[1,50]'" style="width:150px;"/>
	        </td>
	     </tr>
	      <tr>
	     	<td class="TDL">模式名：&nbsp;</td>
	        <td class="TDC">
	      	   <input type="text" id="datasource_schemaname" name="schemaname" class="easyui-validatebox" data-options="validType:['letterOnly','length[0,20]']" style="width:150px;"/>
	        </td>
	        <td class="TDL">服务器名：&nbsp;</td>
	        <td class="TDC">
	      	   <input type="text" id="datasource_dbservername" name="dbservername" class="easyui-validatebox" data-options="validType:['letterOnly','length[0,30]']" style="width:150px;"/>
	        </td>
	     </tr>
	     <tr>
	      <td class="TDL">备 注：&nbsp;</td>
	      <td colspan="3">		      	
	      	<textarea id="datasource_remark" name="remark" class="easyui-validatebox" style="width:444px;height:60px;resize:none;" data-options="validType:'maxLength[100]'"></textarea>
	      </td>
	     </tr> 	
	     <tr><td colspan="4"><div style="height: 4px"></div></td></tr>	    
    </table>	
</form> 